package com.bawei.module_mine.model.entity

import android.os.Parcel
import android.os.Parcelable

/**
 * 红包实体类
 */
data class RedPacketEntity(
    val id: String,                    // 红包ID
    val senderId: String,              // 发送者ID
    val senderName: String,            // 发送者昵称
    val senderAvatar: String,          // 发送者头像
    val amount: Double,                // 红包总金额
    val count: Int,                    // 红包个数
    val remainingCount: Int,           // 剩余个数
    val remainingAmount: Double,       // 剩余金额
    val message: String,               // 红包祝福语
    val createTime: Long,              // 创建时间
    val expireTime: Long,              // 过期时间
    val status: RedPacketStatus,       // 红包状态
    val grabbers: List<RedPacketGrabber> = emptyList() // 抢红包记录
) : Parcelable {
    
    constructor(parcel: Parcel) : this(
        parcel.readString() ?: "",
        parcel.readString() ?: "",
        parcel.readString() ?: "",
        parcel.readString() ?: "",
        parcel.readDouble(),
        parcel.readInt(),
        parcel.readInt(),
        parcel.readDouble(),
        parcel.readString() ?: "",
        parcel.readLong(),
        parcel.readLong(),
        RedPacketStatus.valueOf(parcel.readString() ?: "ACTIVE"),
        parcel.createTypedArrayList(RedPacketGrabber) ?: emptyList()
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(id)
        parcel.writeString(senderId)
        parcel.writeString(senderName)
        parcel.writeString(senderAvatar)
        parcel.writeDouble(amount)
        parcel.writeInt(count)
        parcel.writeInt(remainingCount)
        parcel.writeDouble(remainingAmount)
        parcel.writeString(message)
        parcel.writeLong(createTime)
        parcel.writeLong(expireTime)
        parcel.writeString(status.name)
        parcel.writeTypedList(grabbers)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<RedPacketEntity> {
        override fun createFromParcel(parcel: Parcel): RedPacketEntity {
            return RedPacketEntity(parcel)
        }

        override fun newArray(size: Int): Array<RedPacketEntity?> {
            return arrayOfNulls(size)
        }
    }
}

/**
 * 红包抢取记录
 */
data class RedPacketGrabber(
    val userId: String,                // 用户ID
    val userName: String,              // 用户昵称
    val userAvatar: String,            // 用户头像
    val amount: Double,                // 抢到的金额
    val grabTime: Long,                // 抢取时间
    val isBestLuck: Boolean = false    // 是否手气最佳
) : Parcelable {
    
    constructor(parcel: Parcel) : this(
        parcel.readString() ?: "",
        parcel.readString() ?: "",
        parcel.readString() ?: "",
        parcel.readDouble(),
        parcel.readLong(),
        parcel.readInt() == 1
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(userId)
        parcel.writeString(userName)
        parcel.writeString(userAvatar)
        parcel.writeDouble(amount)
        parcel.writeLong(grabTime)
        parcel.writeInt(if (isBestLuck) 1 else 0)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<RedPacketGrabber> {
        override fun createFromParcel(parcel: Parcel): RedPacketGrabber {
            return RedPacketGrabber(parcel)
        }

        override fun newArray(size: Int): Array<RedPacketGrabber?> {
            return arrayOfNulls(size)
        }
    }
}

/**
 * 红包状态枚举
 */
enum class RedPacketStatus {
    ACTIVE,     // 可抢
    GRABBED,    // 已抢完
    EXPIRED     // 已过期
}

/**
 * WebSocket消息类型
 */
enum class WebSocketMessageType {
    RED_PACKET_SEND,      // 发送红包
    RED_PACKET_GRAB,      // 抢红包
    RED_PACKET_EXPIRE,    // 红包过期
    USER_JOIN,            // 用户进入直播间
    USER_LEAVE,           // 用户离开直播间
    LIVE_START,           // 开始直播
    LIVE_END              // 结束直播
}

/**
 * WebSocket消息实体
 */
data class WebSocketMessage(
    val type: WebSocketMessageType,
    val data: Any,
    val timestamp: Long = System.currentTimeMillis()
)

/**
 * 发送红包请求
 */
data class SendRedPacketRequest(
    val amount: Double,           // 红包金额
    val count: Int,              // 红包个数
    val message: String,         // 祝福语
    val liveRoomId: String       // 直播间ID
)

/**
 * 抢红包请求
 */
data class GrabRedPacketRequest(
    val redPacketId: String,     // 红包ID
    val userId: String           // 用户ID
)

/**
 * 抢红包响应
 */
data class GrabRedPacketResponse(
    val success: Boolean,
    val amount: Double? = null,
    val message: String = "",
    val isBestLuck: Boolean = false
) 